/**
 * Copyright (c) 2002-2008 jNETx.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNETx. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNETx.
 *
 * $Id: $
 *
 */
package pas.book.font;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FontImage extends JFrame {

    public static void main(String[] args) throws Exception {
        new FontImage().setVisible(true);
    }

    public FontImage() throws Exception {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(new FontPanel());
        pack();
    }

    static class FontPanel extends JPanel {
        final BufferedImage image;

        FontPanel() throws IOException {
            image = createImage();
            ImageIO.write(image, "PNG", new FileOutputStream("out/tahoma.png"));
            setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
        }

        @Override
        public void paint(Graphics g) {
            g.drawImage(image, 0, 0, null);
        }

        char[] generateChars() throws UnsupportedEncodingException {
            byte[] bytes = new byte[256 - 32];
            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = (byte) (i + 32);
            }
            return new String(bytes, "Windows-1251").toCharArray();
        }

        BufferedImage createImage() throws IOException {
            char[] chars = generateChars();
            Font font = new Font("Tahoma", Font.PLAIN, 11);

            BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
            Graphics g = image.getGraphics();
            g.setFont(font);
            FontMetrics metrics = g.getFontMetrics(font);
            int i = 0;
            for (char c : chars) {
                i += metrics.charWidth(c);
            }

            DataOutputStream out = new DataOutputStream(new FileOutputStream("out/tahoma.fm"));
            image = new BufferedImage(i, metrics.getHeight(), BufferedImage.TYPE_INT_ARGB);
            g = image.getGraphics();
            g.setFont(font);
            g.setColor(Color.BLACK);
            i = 0;
            int h = metrics.getAscent();
            for (char c : chars) {
                g.drawString(String.valueOf(c), i, h);
                int charWidth = metrics.charWidth(c);
                out.writeByte(charWidth);
                i += charWidth;
            }
            out.close();
            return image;
        }
    }
}
